package com.ichina.chat.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ichina.chat.dao.QuestionDao;
import com.ichina.chat.dao.ReplyDao;
import com.ichina.chat.util.Page;
import com.ichina.chat.vo.ChatRecord;
import com.ichina.chat.vo.Question;
import com.ichina.chat.vo.Reply;
import com.opensymphony.xwork2.ActionSupport;

public class RecordAction extends ActionSupport {
	@Resource
	private QuestionDao questionDao;
	@Resource
	private ReplyDao replyDao;
	private String restult = "";

	public String showAllRecord() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			Page page = new Page();
			String user = "wo";
			page.setPagecode(2);
			page.setPagesize(1);
			page.setPagecount(3);
			page.setSize(2);
			List<Question> qlist = questionDao.queryRecondByUser(user);
			List<Reply> rlist = replyDao.queryReplyRecordByName(user);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			ChatRecord cr;
			List<ChatRecord> clist = new ArrayList<ChatRecord>();
			for (int i = 0; i < qlist.size(); i++) {
				cr = new ChatRecord();
				Question q1 = qlist.get(i);
				cr.setUsername(q1.getUser());
				cr.setRecordTime(q1.getTime());
				cr.setRecordContext(q1.getContext());
				clist.add(cr);
				if (i < qlist.size() - 1) {
					Question q2 = qlist.get(i + 1);
					for (int j = 0; j < rlist.size(); j++) {
						Reply r1 = rlist.get(j);
						if (q1.getTime().getTime() < r1.getTime().getTime()
								&& q2.getTime().getTime() > r1.getTime()
										.getTime()) {
							cr = new ChatRecord();
							cr.setUsername("sss");
							cr.setRecordTime(r1.getTime());
							cr.setRecordContext(r1.getRcontext());
							clist.add(cr);
						}
					}

				} else {
					for (int j = 0; j < rlist.size(); j++) {
						Reply r2 = rlist.get(j);
						if (qlist.get(qlist.size() - 1).getTime().getTime() < r2
								.getTime().getTime()) {
							cr = new ChatRecord();
							cr.setUsername("sss");
							cr.setRecordTime(r2.getTime());
							cr.setRecordContext(r2.getRcontext());
							clist.add(cr);
						}
					}
				}
			}
			List timelist = questionDao.queryRecondByDate();
			request.setAttribute("timelist", timelist);
			
			if (clist.size() == 0) {
				clist = null;
			}
			request.setAttribute("user", user);
			request.setAttribute("clist", clist);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		restult = "showAllrecord";
		return restult;
	}

	public String dateRecord() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String user = "wo";
		String date = request.getParameter("dat");
		try {
			List<Question> qlist = questionDao.queryRecondByUser_date(user,
					date);
			List<Reply> rlist = replyDao
					.queryReplyRecordByName_date(user, date);
			ChatRecord cr;
			List<ChatRecord> clist = new ArrayList<ChatRecord>();
			for (int i = 0; i < qlist.size(); i++) {
				cr = new ChatRecord();
				Question q1 = qlist.get(i);
				cr.setUsername(q1.getUser());
				cr.setRecordTime(q1.getTime());
				cr.setRecordContext(q1.getContext());
				clist.add(cr);
				if (i < qlist.size() - 1) {
					Question q2 = qlist.get(i + 1);
					for (int j = 0; j < rlist.size(); j++) {
						Reply r1 = rlist.get(j);
						if (q1.getTime().getTime() < r1.getTime().getTime()
								&& q2.getTime().getTime() > r1.getTime()
										.getTime()) {
							cr = new ChatRecord();
							cr.setUsername("sss");
							cr.setRecordTime(r1.getTime());
							cr.setRecordContext(r1.getRcontext());
							clist.add(cr);
						}
					}

				} else {
					for (int j = 0; j < rlist.size(); j++) {
						Reply r2 = rlist.get(j);
						if (qlist.get(qlist.size() - 1).getTime().getTime() < r2
								.getTime().getTime()) {
							cr = new ChatRecord();
							cr.setUsername("sss");
							cr.setRecordTime(r2.getTime());
							cr.setRecordContext(r2.getRcontext());
							clist.add(cr);
						}
					}
				}
			}
			if (clist.size() == 0) {
				clist = null;
			}
			request.setAttribute("clist", clist);
			request.setAttribute("user", user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		restult = "dateRecord";
		if (request.getParameter("aa") != null
				&& "aa".equals(request.getParameter("aa"))) {
			restult = "message";
		}
		return restult;
	}
}
